import { useReducer ,useRef} from "react";

function countReducer(state, action) {
    switch (action.type) {
        case 'increment':
            return ++state
        case 'decrement':
            return --state
        default:
            throw new Error() ;
    }
}

export default function Reducer() { 
    
    const [state, dispatch] = useReducer(countReducer, 0)
    const handlerIncrement =() => dispatch({ type: 'increment' })
    const handlerDecrement =() => dispatch({ type: 'decrement' })
    return (
        <>
            <button onClick={handlerDecrement}>-</button>
            {state}
            <button onClick={handlerIncrement}>+</button>
        </>
    );
}